const mongoose = require('mongoose');

// 构建文章模型
const Schema = mongoose.Schema;
const articleSchema = new Schema({
    title: {
        type: String,
        require: true
    },
    content: {
        type: String,
        require: true
    },
    auth: { // 作者
        type: Schema.Types.ObjectId,
        ref: 'user'
    },
    type: { // 所属板块
        type: Schema.Types.ObjectId,
        ref: 'role'
    },
    tags: [String],
    summary: String,
    likes_user: [
        {
            type: Schema.Types.ObjectId,
            ref: 'user'
        }
    ],
    islike: {
        type: Boolean,
        default: false
    },
    jingpin: {
        is: {
            type: Boolean,
            default: false
        },
        upDate: {
            type: Date,
            default: null
        }
    },
    top: {
        is: {
            type: Boolean,
            default: false
        },
        upDate: {
            type: Date,
            default: null
        }
    },
    createDate: {
        type: Date,
        default: Date.now
    },
    upDate: {
        type: Date,
        default: Date.now
    },
    delete: {
        type: Boolean,
        default: false
    }
});

articleSchema.pre('save', function (next) {
    let summary = this.summary;
    let len = summary.length;
    if (len > 100) {
        this.summary = summary.substring(0, 30);
    }
    next();
});

module.exports = mongoose.model('article', articleSchema);
